package cn.xttc.edu.controller;


import cn.xttc.edu.entity.Car;
import cn.xttc.edu.entity.ReturnMsg;
import cn.xttc.edu.entity.vo.CarVo;
import cn.xttc.edu.service.CarService;
import cn.xttc.edu.util.MyUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 杨磊
 * @since 2022-03-09
 */
@RestController
public class CarController {

    @Autowired
    private CarService carService;

    /**
     * 根据id查询车辆信息
     *
     * @param id id
     * @return 包装对象
     */
    @GetMapping("/car/{id}")
    public ReturnMsg getById(@PathVariable("id") int id) {
        Car car = carService.getById(id);
        return ReturnMsg.success(car);
    }

    /**
     * 分页获取所有车辆
     * @param pn    页码
     * @param limit 每页显示行数
     * @param search 搜索参数
     * @return json字符串
     */
    @GetMapping("/car")
    public String getAll(@RequestParam("page") Integer pn,
                         @RequestParam("limit") Integer limit,
                         @RequestParam(value = "search", required = false, defaultValue = "") String search
    ) {
        Page<CarVo> page = new Page<>(pn, limit);
        carService.pageGetAllByCondition(page, search);
        return MyUtil.formatReturnData(page);
    }

    /**
     * 根据车牌号查询车辆是否存在
     * @param licence 车牌号
     * @return 包装对象
     */
    @GetMapping("/car/checkLicence")
    public ReturnMsg getCarByLicence(@RequestParam("licence") String licence) {
        Car car = carService.getOne(new QueryWrapper<Car>().eq("licence", licence));
        if (car == null) {
            return ReturnMsg.success("");
        } else {
            return ReturnMsg.fail("该车辆已存在");
        }
    }

    /**
     * 添加车辆
     *
     * @param car 车辆信息
     * @return 包装对象
     */
    @PostMapping("/car")
    public ReturnMsg addCar(Car car) {
        try {
            carService.save(car);
            return ReturnMsg.success("添加成功");
        } catch (Exception e) {
            return ReturnMsg.fail("添加失败，请联系管理员");
        }
    }

    /**
     * 修改车辆信息
     *
     * @param car 车辆信息
     * @return 包装对象
     */
    @PutMapping("/car/{id}")
    public ReturnMsg editCar(Car car) {
        try {
            carService.updateById(car);
            return ReturnMsg.success("修改成功");
        } catch (Exception e) {
            return ReturnMsg.fail("修改失败");
        }
    }

    /**
     * 批量修改车辆状态
     * @param list 要修改的车辆对象集合
     * @return 包装对象
     */
    @PutMapping("/car")
    public ReturnMsg editBatchCarStatus(@RequestBody List<Car> list) {
        try {
            carService.updateBatchById(list);
            return ReturnMsg.success("修改成功");
        } catch (Exception e) {
            return ReturnMsg.fail("修改失败");
        }
    }

    /**
     * 单个删除和批量删除
     *
     * @param ids id字符串
     * @return 封装对象
     */
    @DeleteMapping("/car/{ids}")
    public ReturnMsg deleteCars(@PathVariable("ids") String ids) {
        if (ids.contains("-")) {
            //批量删除
            List<Integer> delIds = new ArrayList<>();
            String[] strIds = ids.split("-");
            //组装id的集合
            for (String string : strIds) {
                delIds.add(Integer.parseInt(string));
            }
            try {
                carService.removeBatchByIds(delIds);
            } catch (Exception e) {
                return ReturnMsg.fail("删除失败，该车辆已与订单绑定");
            }
        } else {
            //单个删除
            Integer id = Integer.parseInt(ids);
            try {
                carService.removeById(id);
            } catch (Exception e) {
                return ReturnMsg.fail("删除失败，该车辆已与订单绑定");
            }
        }
        return ReturnMsg.success("删除成功");
    }

}

